package cn.tinlan.shopcoin.Utils;

import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.support.annotation.Nullable;
import android.util.Log;

import com.allenliu.versionchecklib.v2.AllenVersionChecker;
import com.allenliu.versionchecklib.v2.builder.DownloadBuilder;
import com.allenliu.versionchecklib.v2.builder.UIData;
import com.allenliu.versionchecklib.v2.callback.ForceUpdateListener;
import com.allenliu.versionchecklib.v2.callback.RequestVersionListener;
import com.zhouyou.http.EasyHttp;
import com.zhouyou.http.callback.CallBackProxy;
import com.zhouyou.http.callback.SimpleCallBack;
import com.zhouyou.http.exception.ApiException;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ListIterator;

import cn.tinlan.shopcoin.ShopCoinApplication;
import cn.tinlan.shopcoin.base.BaseActivity;
import cn.tinlan.shopcoin.base.CustomApiResult;
import cn.tinlan.shopcoin.widget.category.Constants;

/**
 * Created by Mortimer on 2018/12/27. 10:55
 * 佛祖🙏，永无bug。
 */
public class Tools {
    public static String getVersion(Context context) {
        try {

            PackageManager manager =context.getPackageManager();


            PackageInfo info = manager.getPackageInfo(context.getPackageName(), 0);
            String version = info.versionName;
            return version;

        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }





    public static void checkAppUp(){
        EasyHttp.post(Constants.APPUpdate)
                .params("type", "2")
                .execute(new CallBackProxy<CustomApiResult<String>, String>(new SimpleCallBack<String>() {
                    @Override
                    public void onError(ApiException e) {
//                        ToastUtils.show("添加失败");
                    }

                    @Override
                    public void onSuccess(String s) {

                        try {
                            //新建jsonobject 对象 把json格式的字符串 传进来
                            JSONObject jsonObject =new JSONObject(s);
                            Integer code = jsonObject.getInt("code");
                            //把除了头之外的内容读取出来 存为新的jsonobject 对象
                            JSONObject personjsonObject=jsonObject.getJSONObject("data");
                            //把数据中的id读取出来 赋值 给id
                            float  version = (float) personjsonObject.getDouble("version");
                            Log.d("version_______>>>",version+"");
                            //把数据中的name读取出来 赋值 给name
                            String name=personjsonObject.getString("name");

                            String des=personjsonObject.getString("des");

                            String must=personjsonObject.getString("must");

                            String url=personjsonObject.getString("url");

                            toCheckAppUp(version,name,des,must,url);

                        }catch (Exception e) {
                            e.printStackTrace();
                        }

                    }
                }) {
                });
    }


    private static void toCheckAppUp(final float version ,final String name,final String des,final String must,final String url) {

       final float currentVersion =Float.parseFloat( getVersion(ShopCoinApplication.getAppContext()));
        Log.d("currentVersion>>>",currentVersion+"");
            DownloadBuilder builder = AllenVersionChecker
                    .getInstance()
                    .requestVersion()
                    .setRequestUrl(Constants.BathURL+Constants.APPUpdate)
                    .request(new RequestVersionListener() {
                        @Nullable
                        @Override
                        public UIData onRequestVersionSuccess(String result) {
                            if (version > currentVersion) {
                                //如果是最新版本直接return null
                                UIData uiData = UIData
                                        .create()
                                        .setDownloadUrl(url)
                                        .setTitle(name)
                                        .setContent(des);
                                //放一些其他的UI参数，拿到后面自定义界面使用
//                            uiData.getVersionBundle().putString("key", "your value");
                                return uiData;
                            }else {
                                return null;
                            }

                        }

                        @Override
                        public void onRequestVersionFailure(String message) {

                        }
                    });

        if (must.equals("1")){
            builder.setForceUpdateListener(new ForceUpdateListener() {
                @Override
                public void onShouldForceUpdate() {

                    BaseActivity.exitApp();

                }
            });
        }

        builder.executeMission(ShopCoinApplication.getAppContext());
    }


}
